package com.demo.las.repository;

import com.demo.las.entity.PersonalAffair;
import com.demo.las.dto.PersonalAffairDTO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PersonalAffairRepository extends JpaRepository<PersonalAffair, Long> {
    List<PersonalAffair> findByUserIdOrderByStartDateDesc(Long userId);
    boolean existsByAffairCode(String affairCode);
    
    @Query("SELECT new com.demo.las.dto.PersonalAffairDTO(p.id, p.userId, p.affairName, p.affairCode, p.startDate, p.endDate, p.completionRating, p.createTime, p.updateTime) " +
           "FROM PersonalAffair p WHERE p.userId = :userId ORDER BY p.startDate DESC")
    List<PersonalAffairDTO> findAffairsViewByUserId(@Param("userId") Long userId);
}